package com.example.controller.web;

import com.example.common.Result;
import com.example.dto.ArticleFavoriteDTO;
import com.example.handlers.BaseContext;
import com.example.service.ArticleFavoriteService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 文章收藏控制器
 */
@Slf4j
@RestController("webArticleFavoriteController")
@RequestMapping("/article/favorite")
public class ArticleFavoriteController {
    
    @Resource
    private ArticleFavoriteService articleFavoriteService;
    
    /**
     * 收藏/取消收藏文章
     */
    @PostMapping("/toggle")
    public Result<Map<String, Object>> toggleFavorite(@RequestBody ArticleFavoriteDTO articleFavoriteDTO, HttpServletRequest request) {
        try {
            // 获取当前登录用户ID
            Integer userId = BaseContext.getCurrentId();
            if (userId == null) {
                return Result.fail("未登录");
            }
            
            // 检查参数
            if (articleFavoriteDTO == null || articleFavoriteDTO.getArticleId() == null) {
                log.error("收藏文章参数错误: articleFavoriteDTO为空或articleId为空");
                return Result.fail("文章ID不能为空");
            }
            
            log.info("用户收藏/取消收藏文章请求 - 用户ID: {}, 文章ID: {}", userId, articleFavoriteDTO.getArticleId());
            
            // 收藏/取消收藏
            boolean isFavorited = articleFavoriteService.toggleFavorite(articleFavoriteDTO, userId);
            
            // 返回结果
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("isFavorited", isFavorited);
            
            return Result.success(resultMap);
        } catch (Exception e) {
            log.error("收藏/取消收藏文章异常, error: {}", e.getMessage(), e);
            return Result.fail("收藏/取消收藏文章失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询用户是否收藏文章
     */
    @GetMapping("/status")
    public Result<Map<String, Object>> getFavoriteStatus(@RequestParam("articleId") Integer articleId, HttpServletRequest request) {
        try {
            // 获取当前登录用户ID
            Integer userId = BaseContext.getCurrentId();
            if (userId == null) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("isFavorited", false);
                return Result.success(resultMap);
            }
            
            // 检查参数
            if (articleId == null) {
                log.error("查询收藏状态参数错误: articleId为空");
                return Result.fail("文章ID不能为空");
            }
            
            log.info("查询用户是否收藏文章请求 - 用户ID: {}, 文章ID: {}", userId, articleId);
            
            // 查询收藏状态
            boolean isFavorited = articleFavoriteService.hasFavorited(articleId, userId);
            
            // 返回结果
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("isFavorited", isFavorited);
            
            return Result.success(resultMap);
        } catch (Exception e) {
            log.error("查询用户是否收藏文章异常, error: {}", e.getMessage(), e);
            return Result.fail("查询用户是否收藏文章失败: " + e.getMessage());
        }
    }
} 